from flask import Flask, request, jsonify
from flask_cors import CORS
import os
from openai import OpenAI  # 新增OpenAI客户端导入

app = Flask(__name__, static_folder='../AI01', static_url_path='')
CORS(app)


@app.route('/')
def index():
    return app.send_static_file('index.html')


# 初始化OpenAI客户端
client = OpenAI(api_key="sk-dbe4c57f02b64aaf9ac6c937231dce6f", base_url="https://api.deepseek.com")


@app.route('/chat', methods=['POST'])
def chat():
    try:
        data = request.json
        question = data.get('question', '')

        response = client.chat.completions.create(
            model="deepseek-chat",
            messages=[{"role": "user", "content": question}]
        )

        answer = response.choices[0].message.content
        return jsonify({"answer": answer})

    except Exception as e:
        return jsonify({"error": str(e)}), 500


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)  # 确保监听所有网络接口